package com.spring.task3;

import com.alibaba.fastjson2.JSON;
import com.spring.task3.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@SpringBootApplication
@Slf4j
public class SpringTask3Application implements ApplicationRunner {

    @Autowired
    @Qualifier("simpleRestTemplate")
    private RestTemplate simpleRestTemplate;

    @Autowired
    @Qualifier("httpClientRestTemplate")
    private RestTemplate httpClientRestTemplate;

    @Autowired
    @Qualifier("okRestTemplate")
    private RestTemplate okRestTemplate;



    public static void main(String[] args) {
        new SpringApplicationBuilder()
                .sources(SpringTask3Application.class)
                .bannerMode(Banner.Mode.OFF)
                .web(WebApplicationType.NONE)
                .run(args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        List<Coffee> coffees = simpleRestTemplate.getForObject("http://localhost:8081/coffee/getAllForJson", List.class);
        log.info("simpleRestTemplate-JSON接口请求返回的：{}", JSON.toJSONString(coffees));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity httpEntity = new HttpEntity(null, headers);
        ResponseEntity entity = simpleRestTemplate.exchange("http://localhost:8081/coffee/getAllForJson", HttpMethod.GET, httpEntity, List.class);
        log.info("simpleRestTemplate-Xml接口请求返回的：{}",entity.getBody());

        List<Coffee> httpClientCoffees = httpClientRestTemplate.getForObject("http://localhost:8081/coffee/getAllForJson", List.class);
        log.info("httpClientCoffees-JSON接口请求返回的：{}", JSON.toJSONString(httpClientCoffees));

        ParameterizedTypeReference<List<Coffee>> ptr =
                new ParameterizedTypeReference<List<Coffee>>() {};
        ResponseEntity<List<Coffee>> httpClientList = httpClientRestTemplate
                .exchange("http://localhost:8081/coffee/getAllForJson", HttpMethod.GET, null, ptr);
        log.info("customCoffees-JSON接口请求返回的：{}",JSON.toJSONString(httpClientList));

        List<Coffee> okCoffees = okRestTemplate.getForObject("http://localhost:8081/coffee/getAllForJson", List.class);
        log.info("okCoffees-JSON接口请求返回的：{}", JSON.toJSONString(okCoffees));


    }
}
